//
//  ViewController.m
//  徐婷
//
//  Created by MB__Lee on 2019/3/1.
//  Copyright © 2019年 MB__Lee. All rights reserved.
//

#import "ViewController.h"

#import "MBScrollView.h"
#import "MBTableview.h"


#define MBScreen_width [UIScreen mainScreen].bounds.size.width
#define MBScreen_high [UIScreen mainScreen].bounds.size.height
#define Head_high 200

@interface ViewController ()<UIScrollViewDelegate>

//scrollview 上添加headview tableview
@property(nonatomic,strong)MBScrollView *scrollview;
@property(nonatomic,strong)MBTableview *tableview;
@property(nonatomic,strong)UIView *headerview;

//headerview 上添加 悬停view
@property(nonatomic,strong)UILabel *oView;

@property(nonatomic,assign)BOOL canScroll;

@end

@implementation ViewController
-(MBScrollView *)scrollview{
    if (!_scrollview) {
    
        MBScrollView *scrollview = [[MBScrollView alloc] initWithFrame:self.view.bounds];
        _scrollview = scrollview;
        scrollview.delegate = self;
        scrollview.contentSize = CGSizeMake(MBScreen_width, MBScreen_high + Head_high);
    }
    return  _scrollview;
}
-(MBTableview *)tableview{
    if (!_tableview) {
        CGRect rect =  CGRectMake(0, Head_high, MBScreen_width, MBScreen_high);
        MBTableview *tableview = [[MBTableview alloc] initWithFrame:rect style:UITableViewStylePlain];
        _tableview = tableview;
        
    }
    return  _tableview;
}
-(UIView *)headerview{
    if (!_headerview) {
        _headerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MBScreen_width, Head_high)];
        _headerview.backgroundColor = [UIColor grayColor];
    }
    return  _headerview;
}
-(UIView *)oView{
    if (!_oView) {
        _oView = [[UILabel alloc] initWithFrame:CGRectMake(0, Head_high-30, MBScreen_width, 30)];
        _oView.backgroundColor = [UIColor yellowColor];
        _oView.text = @"悬停部分";
    }
    return  _oView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    视图之间的添加
    [self.view addSubview:self.scrollview];
    [self.scrollview addSubview:self.headerview];
    [self.scrollview addSubview:self.tableview];
    
    [self.headerview addSubview:self.oView];
    
//    悬停核心代码
    _canScroll = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(levelAction) name:@"leTop" object:nil];
    
}
-(void)levelAction{
    _canScroll = YES;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint offset = scrollView.contentOffset;
    CGFloat y = offset.y;
    if (_canScroll == YES) {
        if (y > Head_high-30) {
            offset.y = Head_high -30;
            [scrollView setContentOffset:offset];
            _canScroll = NO;
            [[NSNotificationCenter defaultCenter]postNotificationName:@"atTop" object:nil];
        }
        
    }else{
        [scrollView setContentOffset:CGPointMake(0, Head_high-30)];
        
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
